import os
import json
import shutil


def cook_network(net):
    file_path = '../raw_data/throughput/' + net
    files = os.listdir(file_path)
    write_path = './Network_throughput/'
    if net == '4Glogs':
        # 单位 s, kbps, kbps, kbps, kbps, kbps, kbps
        write_path += '4G_'
    for f in files:
        with open(file_path + '/' + f, 'r') as f_tmp:
            throughput_tmp = json.load(f_tmp)

        with open(write_path + f.strip('.json') + '.txt', 'w') as w:
            time_stamp = 0
            for i in range(len(throughput_tmp)):
                w.write(format(time_stamp/1000, '.3f') + ',' + str(throughput_tmp[i]['bandwidth_kbps']*1000)
                        + ',' + str(throughput_tmp[i]['bandwidth_kbps']*1000)
                        + ',' + str(throughput_tmp[i]['bandwidth_kbps']*1000)
                        + ',' + str(throughput_tmp[i]['bandwidth_kbps']*1000)
                        + ',' + str(throughput_tmp[i]['bandwidth_kbps']*1000)
                        + ',' + str(throughput_tmp[i]['bandwidth_kbps']*1000) + '\n')
                time_stamp += throughput_tmp[i]['duration_ms']


def load_data_all(self):
    # self.dirs = os.listdir("D:\Project_Python\ABR\option-critic-pytorch-master\data_train")
    self.dirs = os.listdir("../raw_data/from_the_system")

    # 全读进内存
    for i in range(len(self.dirs)):
        dir_path = "../raw_data/from_the_system/" + self.dirs[i] + "/"
        self.dataset_all[i].name = dir_path
        self.dataset_all[i].Fetch_data_from_file(dir_path, "bitrateLevel.txt")
        self.dataset_all[i].Fetch_data_from_file(dir_path, "bitrateInfo.txt")
        self.dataset_all[i].Fetch_data_from_file(dir_path, "throughput.txt")
        self.dataset_all[i].Fetch_data_from_file(dir_path, "buffer.txt")
        self.dataset_all[i].Fetch_data_from_file(dir_path, "FovRecord.txt")
        self.dataset_all[i].Fetch_data_from_file(dir_path, "pause_total.txt")
        self.dataset_all[i].Fetch_data_from_file(dir_path, "rebuffer_time.txt")


if __name__ == '__main__':
    cook_network('4Glogs')
